using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using GameFramework;
using GameFramework.Event;
using GameFramework.Network;
using UnityEngine;
using UnityGameFramework.Runtime;
using AddressFamily = GameFramework.Network.AddressFamily;

//自动生成于：2020/11/10 16:27:18
namespace StarForce
{

	public partial class MenuPage : UGuiForm
	{

		protected override void OnInit(object userdata)
		{
			base.OnInit(userdata);

			GetBindComponents(gameObject);

			m_Btn_FlyText.onClick.AddListener(OpenFlyTextPage);
			
			m_Btn_List.onClick.AddListener(OpenListPage);
			
			m_Btn_LoopList.onClick.AddListener(OpenLoopListPage);
			
//			m_Btn_ConnectServer.onClick.AddListener(ConnectServer);
//			
//			m_Btn_SendMsg.onClick.AddListener(OnSendMsg);
			
			m_Btn_RedDot.onClick.AddListener(OnRedDot);
			
			m_Btn_Eliminate.onClick.AddListener(OnStartEliminateGame);
			
			m_Btn_Web.onClick.AddListener(OnHttp);
			
			m_Btn_TypeWriter.onClick.AddListener(OnOpenTypeWriterPage);
			
			m_Btn_Watermelon.onClick.AddListener(OnOpenWatermelonMainPage);
		}

		private void OnOpenWatermelonMainPage()
		{
			GameEntry.Event.Fire(this,ReferencePool.Acquire<GameStartEventArgs>().Fill(GameMode.Watermelon));
		}

		private void OnOpenTypeWriterPage()
		{
			GameEntry.UI.OpenUIForm(UIFormId.TypeWriterPage, this);
		}


		/// <summary>
		/// Http 传输
		/// </summary>
		private void OnHttp()
		{
			Dictionary<string, object> postargs = new Dictionary<string, object>();
			postargs.Add("platform", "s1");
			postargs.Add("uid", "ccc");

			GameEntry.Http.Post(postargs, "serverList");
		}

		/// <summary>
		/// 开始三消游戏
		/// </summary>
		private void OnStartEliminateGame()
		{
			Close();
			GameEntry.Event.Fire(this,ReferencePool.Acquire<GameStartEventArgs>().Fill(GameMode.Eliminate));
		}

		protected override void OnOpen(object userData)
		{
			base.OnOpen(userData);
			GameEntry.Gate.OnConnected += OnConnect;
			GameEntry.Http.Subscribe("userLogin",OnUserLogin);
			GameEntry.Http.Subscribe("serverList",OnServerList);
		}

		protected override void OnClose(bool isShutdown, object userData)
		{
			base.OnClose(isShutdown, userData);
			GameEntry.Http.UnSubscribe("userLogin",OnUserLogin);
			GameEntry.Http.UnSubscribe("serverList",OnServerList);
		}
		

		private void OnServerList(object sender, GameEventArgs e)
		{
			WebResponseEventArgs result = (WebResponseEventArgs) e;
			
			Log.Info("---:"+result.JsonString);
		}

		private void OnUserLogin(object sender, GameEventArgs e)
		{
			WebResponseEventArgs result = (WebResponseEventArgs) e;
			
			Log.Info("---:"+result.JsonString);
		}

		private void OnConnect(INetworkChannel arg1, object arg2)
		{
			Log.Info("连接成功");
		}

		/// <summary>
		/// 打开无限滚动列表
		/// </summary>
		private void OpenLoopListPage()
		{
			GameEntry.UI.OpenUIForm(UIFormId.LoopListPage, this);
		}

		/// <summary>
		/// 打开一般列表
		/// </summary>
		private void OpenListPage()
		{
			GameEntry.UI.OpenUIForm(UIFormId.ListPage, this);
		}

		/// <summary>
		/// 打开飘字效果界面
		/// </summary>
		private void OpenFlyTextPage()
		{
			GameEntry.UI.OpenUIForm(UIFormId.FlyTextPage, this);
		}
		
		

		/// <summary>
		/// 打开红点界面
		/// </summary>
		private void OnRedDot()
		{
			GameEntry.UI.OpenUIForm(UIFormId.RedDotPage, this);
		}
		
		
		/// <summary>
		/// 连接服务器
		/// </summary>
		private void ConnectServer()
		{
			if (GameEntry.Gate.Connected)
			{
				Log.Info("已经连接了服务器");
				return;
			}
			GameEntry.Gate.Connect();

			
//			TcpClient client = new TcpClient();
//			try
//			{
//				client.Connect(IPAddress.Parse("127.0.0.1"), 10001);//同步方法，连接成功、抛出异常、服务器不存在等之前程序会被阻塞
//			}
//			catch (Exception ex)
//			{
//				Debug.Log("客户端连接异常："+ex.Message);
//			}
//			Debug.Log("LocalEndPoint = " + client.Client.LocalEndPoint + ". RemoteEndPoint = " + client.Client.RemoteEndPoint);
// 
//			//客户端发送数据部分
//			string msg = "hello server";
//			NetworkStream streamToServer = client.GetStream();//获得客户端的流
//			byte[] buffer = Encoding.Unicode.GetBytes(msg);//将字符串转化为二进制
//			streamToServer.Write(buffer, 0, buffer.Length);//将转换好的二进制数据写入流中并发送
//			Debug.Log("发出消息："+msg);
		}
		
		
		/// <summary>
		/// 给服务器发送消息
		/// </summary>
		private void OnSendMsg()
		{
			Demo1 demo1 = ReferencePool.Acquire<Demo1>();
			
			GameEntry.Gate.Send(demo1);

		}

	}
}  

